package net.bwie.jtp.ads.udf;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用HuTool类库中UserAgentUtil工具类解析UA，获取浏览器信息
 * @author xuanyu
 * @date 2025/7/11
 */
public class ParseUaToBrowser  extends UDF {

	public Map<String, String> evaluate(String userAgent) throws Exception{
		// 定义集合
		Map<String, String> map = new HashMap<String, String>() ;

		// 1. 解析
		UserAgent ua = UserAgentUtil.parse(userAgent);

		// 2.解析
		// {os=Linux, is_mobile=0, engine=Gecko, browser=Firefox, os_version=null, browser_version=3.5.3, engine_version=20090824, platform=Linux}
		map.put("browser", ua.getBrowser().toString());
		map.put("browser_version", ua.getVersion());
		map.put("engine", ua.getEngine().toString());
		map.put("engine_version", ua.getEngineVersion());
		map.put("os", ua.getOs().toString());
		map.put("os_version", ua.getOsVersion());
		map.put("platform", ua.getPlatform().toString());
		map.put("is_mobile", ua.isMobile() ? "1" : "0");

		return map ;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(new ParseUaToBrowser().evaluate("Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 GTB5"));
	}

}
